﻿<div ng-controller="LockToOrderC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">转成计划单</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div ng-show="isshow" class="table-responsive">
                <form name="form" class="bs-example form-horizontal">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">客户名称</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" class="form-control" readonly="readonly" value="{{toorderdata.Customer.Name}}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">公司户头</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" class="form-control" readonly="readonly" value="{{toorderdata.Account.Name}}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-12  control-label" style="padding-right: 0px;">目的地</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-12">
                                    <div class="input-group ">
                                        <span class="input-group-addon">省</span>
                                        <select required="required" class="form-control" ng-options="x.FillName for x in items" ng-model="selectedDistrict1"></select>
                                        <span class="input-group-addon">市</span>
                                        <select required="required" class="form-control" ng-model="orderdata.DistrictId" name="District">
                                            <option ng-repeat="y in selectedDistrict1.Children" value="{{y.Id}}">{{y.Name}}</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">具体地址</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input ng-model="orderdata.Address" type="text" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="checkbox" style="padding-left: 15px">
                                    <label for="xiankuan" style="margin-right: 10px;padding: 0px">是否先款</label>
                                    <label class="i-checks">
                                        <input type="checkbox" id="xiankuan" disabled="disabled" ng-model="toorderdata.IsFullPayment">
                                        <i></i>
                                    </label>
                                    <label for="zipai" style="margin-right: 10px;">是否自派</label>
                                    <label class="i-checks">
                                        <input type="checkbox" id="zipai" ng-model="orderdata.IsDelivery">
                                        <i></i>
                                    </label>
                                </div>
                            </div>
                            <div class="form-group" ng-if="!(orderdata.IsDelivery || toorderdata.Account.IsCash)">
                                <div class="checkbox" style="padding-left: 15px">
                                    <label for="gonghu" style="margin-right: 10px">运费是否公户</label>
                                    <label class="i-checks">
                                        <input type="checkbox" id="gonghu" ng-model="orderdata.IsPublicDelivery">
                                        <i></i>
                                    </label>
                                </div>
                            </div>
                            <div class="form-group" ng-if="!orderdata.IsDelivery">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">每吨运费</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input ng-change="sum()" required ng-model="orderdata.DeliveryPrice" type="number" min="0" placeholder="请输入每吨运费" class="form-control">
                                </div>
                            </div>
                            <div class="form-group" ng-if="orderdata.IsDelivery">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">车辆信息</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" ng-pattern="/^[a-zA-Z0-9\u4e00-\u9fa5,]+$/" ng-model="orderdata.VehicleInfos" class="form-control" placeholder="请输入车辆信息(每条信息以英文逗号分隔)" maxlength="100">
                                </div>
                            </div>
                            <div class="form-group" ng-if="orderdata.IsDelivery">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">联系人</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" ng-pattern="/^[a-zA-Z0-9\u4e00-\u9fa5,]+$/" ng-model="orderdata.Contact" class="form-control" placeholder="请输入联系人信息(每条信息以英文逗号分隔)">
                                </div>
                            </div>
                            <div class="form-group" ng-if="orderdata.IsDelivery">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">联系方式</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" ng-model="orderdata.ContactPhone" class="form-control" ng-pattern="/^[0-9,]+$/" placeholder="请输入联系人手机号(每条信息以英文逗号分隔)">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">备注信息</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input ng-model="orderdata.Remarks" type="text" class="form-control" placeholder="请输入备注信息（100字内）" maxlength="100">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">单价优惠</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" class="form-control" readonly="readonly" value="{{toorderdata.ApplyDiscount}}">
                                    <!--<input ng-change="sum()" ng-model="orderdata.ApplyDiscount" type="number" min="0" placeholder="申请单价优惠 元/吨  (默认为0)" class="form-control">-->
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-1 col-md-2 col-sm-2 col-xs-3  control-label" style="padding-right: 0px;">供应商</label>
                                <div class="col-lg-11 col-md-10 col-sm-10 col-xs-9">
                                    <input type="text" class="form-control" readonly="readonly" value="{{toorderdata.Provider.Name}}">
                                    <!--<select ng-change="change1()" id="depot" class="form-control cangku" ng-options="x.Name for x in cangku" ng-model="Providerdata" name="cangku"></select>-->
                                </div>
                            </div>
                            <div ng-if="toorderdata.IsFullPayment" class="row text-left container-fluid">
                                <span>先款优惠：{{xiankuan | number:2}}/吨</span>
                            </div>
                            <div ng-if="toorderdata.Account.IsCash" class="row text-left container-fluid">
                                <span>非标优惠：{{feibiao | number:2}}/吨</span>
                            </div>
                            <div class="row text-left container-fluid">
                                <span>锁价单剩余额度：{{Surplus | number:2}}￥</span>
                            </div>
                        </div>
                        <div class="container-fluid bg-light lter">
                            <div class="row" style="height: 40px;">
                                <div class="col-xs-6 col-md-6 text-left">
                                    <strong style="display: inline-block;line-height: 40px">购物车</strong>
                                </div>
                                <a>
                                    <div ng-click="xxx()" class="col-xs-6 col-md-6 text-right" style="display: inline-block;line-height: 100%;height: 100%">
                                        <span style="display: inline-block;line-height: 40px">
                                            去选商品&nbsp;&nbsp;
                                            <i class="fa fa-chevron-right text"></i>
                                        </span>
                                    </div>
                                </a>
                            </div>
                        </div>
                        <div class="container-fluid" style="padding-top: 10px">
                            <div class="panel panel-default" ng-repeat="item in orderdata.CommodityInfos" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>钢厂：{{item.Commodity.Brand}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-9 col-md-9">
                                            <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                            <div>单价：{{item.Price | number:2}}/t</div>
                                            <div>
                                                件数：
                                                <input ng-change="sum()" type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"  min="1" ng-model="item.Count">
                                            </div>
                                            <div>
                                                <span style="font-size: 0.8em; color: #999;" ng-if="item.Amount-(item.Count*item.Commodity.Weight)>0">剩余库存数量：{{item.Amount-(item.Count*item.Commodity.Weight) | number:3}} t</span>
                                                <span style="font-size: 0.8em; color: red" ng-if="item.Amount-(item.Count*item.Commodity.Weight)<0">暂无库存，无法购买！</span>
                                            </div>
                                            <div>总数量：{{item.Count | number:2}}*{{item.Commodity.Weight | number:3}}t={{(item.Count*item.Commodity.Weight).toFixed(3)}}t</div>
                                        </div>
                                        <div class="col-xs-3 col-md-3" style="text-align: center;">
                                            <button ng-disabled="item.Amount-(item.Count*item.Commodity.Weight)<0" type="button" class="btn btn-sm btn-default" ng-click="Countadd(item)" style="margin: 5px">
                                                <i class="fa fa-plus text"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-default" ng-click="Countdeduct(item)" style="margin: 5px">
                                                <i class="fa fa-minus text"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" ng-click="deduct(item)" style="margin: 5px">
                                                删除
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                <div class="panel-footer">
                                    <div>总价：{{item.Count}}*{{item.Price | number:2}}*{{item.Commodity.Weight | number:3}}t={{(item.Count*item.Price*item.Commodity.Weight) | number:2}}</div>
                                </div>
                            </div>

                            <div class="row text-left container-fluid">
                                <span>原始价格：{{originalTotal | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid" ng-if="toorderdata.IsFullPayment && orderdata.Total > 0">
                                <span>先款优惠：{{originalxian | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid" ng-if="toorderdata.Account.IsCash && orderdata.Total > 0">
                                <span>非标优惠：{{originalfei | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid" ng-if="toorderdata.ApplyDiscount > 0 && orderdata.Total > 0">
                                <span>单价优惠：{{-originalApplyDiscount | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid" ng-if="orderdata.BargainingAmount > 0 && orderdata.Total > 0">
                                <span>总价优惠：{{orderdata.BargainingAmount | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid">
                                <span>总价：{{orderdata.Total | number:2}}</span>
                                <!--<h4 style="display: inline-block">总价：{{orderdata.Total}}</h4>-->
                            </div>
                            <div ng-if="orderdata.Total > Surplus" class="row text-left container-fluid">
                                <span style="color: red;font-size: 12px;"><i>超出锁价额度</i></span>
                            </div>
                            <div class="row text-left container-fluid">
                                <span>预计运费价格：{{newWeight * orderdata.DeliveryPrice | number:2}}</span>
                                <!--<h4 style="display: inline-block">预计运费价格：{{newWeight * orderdata.DeliveryPrice}}</h4>-->
                            </div>

                        </div>
                        <div class="row text-center" style="height: 40px;">
                            <a>
                                <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                    <button type="button" style="width: 100%;height: 100%" class="btn btn-default" ng-click="abolish()">返回列表页</button>
                                </div>
                                <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                    <button type="button" style="width: 100%;height: 100%" class="btn btn-success" ng-disabled="form.$invalid || orderdata.Total < 0 || orderdata.Total > Surplus" ng-click="ordersub()">提交计划单</button>
                                </div>
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <div ng-show="!isshow">
                <div class="container-fluid">
                    <div class="row" style="height: 40px;">
                        <a>
                            <div ng-click="xxx()" class="col-xs-6 col-md-6 text-left" style="display: inline-block;line-height: 100%;height: 100%">
                                <span style="display: inline-block;line-height: 40px">
                                    <i class="fa fa-chevron-left text"></i>&nbsp;&nbsp;返回订单
                                </span>
                            </div>
                        </a>
                        <!--<div class="col-xs-6 col-md-6 text-center">-->
                        <!--<select style="" class="btn btn-default" ng-model="findName" ng-change="genre(findName)">-->
                        <!--<option value="盘螺">盘螺</option>-->
                        <!--<option value="线材">线材</option>-->
                        <!--<option value="钢坯">钢坯</option>-->
                        <!--<option value="螺纹钢">螺纹钢</option>-->
                        <!--</select>-->
                        <!--</div>-->
                    </div>
                </div>

                <div class="panel panel-default" ng-repeat="item in DepotCommodity" style="margin-bottom: 5px">
                    <div class="panel-heading">
                        <div>品牌：{{item.Commodity.Brand}}</div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-xs-9 col-md-9">
                                <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                <div>价格：{{item.Price | number:2}}￥</div>
                                <div>库存：{{item.Amount | number:3}}（t）</div>
                            </div>
                            <div class="col-xs-3 col-md-3" style="text-align: center; padding-top: 30px">
                                <button type="button" ng-show="!item.isadd" class="btn btn-sm btn-default" ng-click="add(item)">
                                    <i class="fa fa-plus text"></i>
                                    <span class="text">添加</span>
                                </button>
                                <button type="button" ng-show="item.isadd" class="btn btn-sm btn-default" ng-click="deduct(item)">
                                    <i class="fa fa-minus text"></i>
                                    <span class="text">取消</span>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app') //锁价单转计划单控制器
            .controller("LockToOrderC", function ($scope, $state, $http, $stateParams) {
                $scope.isshow = true;
                $scope.orderdata = {
                    CommodityInfos: [],
                    Total: 0,
                    DeliveryPrice: 0,
                    BargainingAmount: 0
                };
                $scope.Surplus = 0;
                $scope.orderiddetail = JSON.parse($stateParams.orderid);
                $scope.selectedDistrict1 = "";
                //请求数据
                $http({
                    url: "../api/LockOrder/AddOrder?id=" + $scope.orderiddetail,
                    method: "POST"
                })
                        .then(function (res) {
                            $scope.DepotCommodity = res.data.DepotCommodity;
                            $scope.toorderdata = res.data.Order;
                            $scope.Surplus = res.data.Surplus;
                            $scope.xiankuan = $scope.toorderdata.Provider.FullPaymentDiscount;
                            $scope.feibiao = $scope.toorderdata.Provider.NonstandardDiscount;
                            console.log(res.data)
                        })
                        .then(function (error) {
                            if (error) {
                                console.log("error");
                            }
                        });
                //获取地市
                $http({
                    url: "../api/District",
                    method: "GET"
                })
                        .then(function (res) {
                            $scope.items = res.data;
                        })
                        .then(function (error) {
                            if (error) {
                                console.log("error");
                            }
                        });
                //计算总价方法
                $scope.sum = function () {
                    if ($scope.orderdata.IsDelivery) {
                        $scope.orderdata.DeliveryPrice = 0;
                    }
                    $scope.orderdata.Total = 0;
                    $scope.Weight = 0;
                    $scope.newWeight = 0;
                    $scope.originalApplyDiscount = 0;
                    $scope.originalxian = 0;
                    $scope.originalfei = 0;
                    for (var i = 0; i < $scope.orderdata.CommodityInfos.length; i++) {
                        $scope.Weight = $scope.orderdata.CommodityInfos[i].Count * $scope.orderdata.CommodityInfos[i].Commodity.Weight;
                        $scope.orderdata.Total += $scope.Weight * $scope.orderdata.CommodityInfos[i].Price;
                        $scope.originalTotal = $scope.orderdata.Total;

                        $scope.newWeight += $scope.orderdata.CommodityInfos[i].Count * $scope.orderdata.CommodityInfos[i].Commodity.Weight;
                    }
                    //先款优惠
                    if ($scope.toorderdata.IsFullPayment && $scope.orderdata.Total > 0) {
                        $scope.originalxian += $scope.xiankuan * $scope.newWeight;
                    }
                    //非标优惠
                    if ($scope.toorderdata.Account.IsCash && $scope.orderdata.Total > 0) {
                        $scope.originalfei += $scope.feibiao * $scope.newWeight;
                    }
                    //申请单价优惠
                    if ($scope.toorderdata.ApplyDiscount > 0 && $scope.orderdata.Total > 0) {
                        $scope.originalApplyDiscount -= $scope.toorderdata.ApplyDiscount * $scope.newWeight;
                    }
                    $scope.orderdata.Total = $scope.orderdata.Total - $scope.originalxian - $scope.originalfei + $scope.originalApplyDiscount;
                    $scope.orderdata.Total = $scope.orderdata.Total.toFixed(2);
                };
                //提交编辑结果
                $scope.xxx = function () {
                    $scope.sum();
                    $scope.isshow = !$scope.isshow;
                };
                //购物车增加数量
                $scope.Countadd = function (item) {
                    item.Count += 1;
                    $scope.sum();
                };
                //购物车减少数量
                $scope.Countdeduct = function (item) {
                    if (item.Count > 0) {
                        item.Count -= 1
                        $scope.sum();
                    }
                };
                //添加到购物车
                $scope.add = function (item) {
                    for (var i = 0; i < $scope.orderdata.CommodityInfos.length; i++) {
                        if ($scope.orderdata.CommodityInfos[i].CommodityId == item.CommodityId) {
                            return;
                        }
                    }
                    item.isadd = true;
                    item.Count = 1;
                    $scope.sum();
                    $scope.orderdata.CommodityInfos.push(item);
                };
                //从购物车删除
                $scope.deduct = function (item) {
                    item.isadd = false;
                    item.Count = 0;
                    $scope.sum();
                    for (var i = 0; i < $scope.orderdata.CommodityInfos.length; i++) {
                        if ($scope.orderdata.CommodityInfos[i].CommodityId == item.CommodityId) {
                            $scope.orderdata.CommodityInfos.splice(i, 1);
                        }
                    }
                };
                //购物车增加数量
                $scope.Countadd = function (item) {
//                    console.log(item);
                    item.Count += 1;
                    $scope.sum();
                };
                //购物车减少数量
                $scope.Countdeduct = function (item) {
                    if (item.Count > 0) {
                        item.Count -= 1;
                        $scope.sum();
                    }
                };
                //提交计划单
                $scope.ordersub = function () {
                    $scope.orderdata.LockOrderId = $scope.orderiddetail;
                    $scope.orderdata.CustomerId = $scope.toorderdata.Customer.Id;
                    $scope.orderdata.ProviderId = $scope.toorderdata.Provider.Id;
                    $scope.orderdata.AccountId = $scope.toorderdata.Account.Id;
//                    $scope.orderdata.CommodityInfos = $scope.toorderdata.CommodityInfos;
                    $scope.orderdata.OrderType = $scope.toorderdata.OrderType;

                    console.log($scope.orderdata);
                    $http.post('../api/Order/Add', $scope.orderdata)
                            .success(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    console.log(res);
                                    alert("已提交");
                                    $state.go('app.Order_MyorderList');
                                }

                            })
                            .error(function (error) {
                                if (error) {
                                    alert(error.msg);
                                }
                            });
                };
                //返回上一页
                $scope.abolish = function () {
                    $state.go('app.Order_orderList');
                };
            })
</script>